<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    ProductController
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * @category   Zend
 * @package    ProductController
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Storefront_ProductController extends Zend_Controller_Action
{

    /**
     * The logger
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * Id of language
     * @var integer
     */
    protected $_languageId;

    /**
     * The database model for category
     * @var Storefront_Model_Category
     */
    protected $_categoryModel;

    /**
     * Initialize the controller
     */
    public function init()
    {
        // Get logger
        $this->_logger = Zend_Registry::get('log');

        // Get language from locale
        $locale = Zend_Registry::get('Zend_Locale');
        $languageModel = new Storefront_Model_Language();
        $this->_languageId = $languageModel->findIdByShortCode($locale->getLanguage());
        $this->view->language = $locale->getLanguage();

        // Get category tree
        $this->_categoryModel = new Storefront_Model_Category();
        $this->view->categoryTree = $this->_categoryModel->getTree($this->_languageId);

        $this->view->shopMode = Zend_Registry::get('config')->shopMode;

        // Get cart
        $cartModel = new Storefront_Model_Cart();
        $this->view->cartCount = $cartModel->count();

        // Get current url without language
        $this->view->currentUrl = str_replace('/' . $locale->getLanguage(), '', $this->_request->getRequestUri());

        // Form for search
        $form = new Storefront_Model_FormSearch();
        $form->setAction('/' . $locale->getLanguage() . '/search');
        $this->view->formSearch = $form;

        $this->view->shopName = Zend_Registry::get('config')->shopName;
        $this->view->shopTitle = Zend_Registry::get('config')->shopTitle;
        $this->view->shopCity = Zend_Registry::get('config')->shopCity;
    }

    /**
     * Action for view
     */
    public function viewAction()
    {
        // Get product ID from reuqets
        $productId = $this->_request->getParam('id');

        // Get product info
        $productModel = new Storefront_Model_Product();
        $product = $productModel->getByIdAndLanguage($productId, $this->_languageId);
        $this->view->product = $product;

        // Check product info
        if (!$this->view->product) {
            $this->_redirect();
        }

        // Get product features
        $productFeatureModel = new Storefront_Model_ProductFeature();
        $productFeatures = $productFeatureModel->getAllByProductAndLanguage($productId, $this->_languageId);
        $productFeatureGroups = array();

        // Group product features
        for ($i = 0; $i < count($productFeatures); $i++) {
            $productFeature = $productFeatures[$i];

            // Create feature group if does not exists
            if (!isset($productFeatureGroups[$productFeature->feature_group_name])) {
                $productFeatureGroups[$productFeature->feature_group_name] = array();
            }

            // Add specification to feature group 
            $specfication = array(
                'name' => $productFeature->feature_name,
                'value' => $productFeature->presentation_value
            );
            array_push($productFeatureGroups[$productFeature->feature_group_name], (object) $specfication);
        }
        $this->view->productFeatureGroups = $productFeatureGroups;
        $this->view->productFeaturesCount = count($productFeatures) + count($productFeatureGroups);

        // Get categories for breadcrumb
        $categoryModel = new Storefront_Model_Category();
        $this->view->breadcrumb = $categoryModel->getParentCategories($product->category_id, $this->_languageId);

        // Get time of execution
        $startTime = Zend_Registry::get('startTime');
        $totalTime = round(microtime(true) - $startTime, 3);
        $this->_logger->debug(__METHOD__." spent $totalTime s");
    }

}

